package cdbft

import (
	"blockchain/public/entity"
	"blockchain/public/function"
	"encoding/json"
)

func pre_prepareDelegate(priSeq int){
	var trades [TradeSize]entity.Trade
	var pre_prepare pre_prepareMessage
	pre_prepare.view=View
	pre_prepare.sequence=currentTradeSeq
	pre_prepare.priSeq=priSeq
	t:=currentRequest.tradeSet
	t=orderTrades(t)
	if t!=trades{
		trades=t
		pre_prepare.trades=trades
		data,_:=json.Marshal(pre_prepare.trades)
		pre_prepare.sign=function.Sign(data,pre_prepare.priSeq)
		var flagPre_prepared =make(chan bool)
		go priSendPre_prepareDelegate(pre_prepare,flagPre_prepared)
		<-flagPre_prepared
		//fmt.Println(pre_prepare.sign)
	}
}

func priSendPre_prepareDelegate(pre_prepare pre_prepareMessage,flagPre_prepared chan bool){
	for i:=0;i<groupNum;i++{
		select {
		case chanRecPre_prepare[delegate[i]]<-pre_prepare:close(chanRecPre_prepare[delegate[i]])
		default:
		}
	}
	flagPre_prepared<-true
	close(flagPre_prepared)
}
